package com.aidysoft.practice.pdf;


import com.aidysoft.practice.easypoi.utils.ItextpdfUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;


@RestController
public class PdfController {

    //打印pdf
    @RequestMapping("/printPdf")
    public String printPdf() throws Exception {
        String fileName = "D:\\testTable3.pdf";

        Font chapterFont = ItextpdfUtil.createChineseFont(20, Font.BOLD, new BaseColor(0, 0, 255));//文章标题字体
        Font sectionFont = ItextpdfUtil.createChineseFont(16, Font.BOLD, new BaseColor(0, 0, 255));//文章小节字体
        Font textFont = ItextpdfUtil.createChineseFont(10, Font.NORMAL, new BaseColor(0, 0, 0));//小节内容字体


        FileOutputStream out = new FileOutputStream(new File(fileName));

        //页面大小
        Rectangle rect = new Rectangle(PageSize.B5.rotate());
        //页面背景色
//        rect.setBackgroundColor(BaseColor.WHITE);

        Document document = new Document(rect, 50, 50, 50, 50);
        PdfWriter writer = PdfWriter.getInstance(document, out);

        document.open();


        //创建章节
        Chapter chapter = ItextpdfUtil.createChapter("章1", 1, 1, 0, chapterFont);

        //创建新页面
        document.newPage();
        //水印
        ItextpdfUtil.getWaterText(writer);

        Section section1 = ItextpdfUtil.createSection(chapter, "节1.1", sectionFont, 0);
        Phrase text1 = ItextpdfUtil.createPhrase("Hello,欢迎使用iText", textFont);
        section1.add(text1);


        PdfPTable table1 = ItextpdfUtil.createPdfPTable(3);
        PdfPCell titleCell1 = ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("this is title")));
        titleCell1.setColspan(3);
        table1.addCell(titleCell1);

        for (int j = 0; j < 5; j++) {
            table1.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("two" + j))));
            table1.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("two" + j))));
            table1.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("three" + j))));
        }

        table1.setSpacingBefore(20);
        section1.add(table1);
        document.add(section1);

        //文档属性
        document.addTitle("Title@sample");
        document.addAuthor("Author@rensanning");
        document.addSubject("Subject@iText sample");
        document.addKeywords("Keywords@iText");
        document.addCreator("Creator@iText");

        //PDF版本(默认1.4)
//        writer.setPdfVersion(PdfWriter.PDF_VERSION_1_2);

        // 设置密码为："World"
         /* writer.setEncryption("aidy".getBytes(), "aidy".getBytes(),
                    PdfWriter.ALLOW_SCREENREADERS,
                    PdfWriter.STANDARD_ENCRYPTION_128);
*/
        document.newPage();
        ItextpdfUtil.getWaterPic(writer);

        Paragraph paragraph2 = ItextpdfUtil.createParagraph("第二页这是一段段落", sectionFont);
        Paragraph paragraph3 = ItextpdfUtil.createParagraph("第二页这是一段段落", sectionFont);
        Paragraph paragraph4 = ItextpdfUtil.createParagraph("第二页这是一段段落", sectionFont);
        document.add(paragraph2);
        document.add(paragraph3);
        document.add(paragraph4);

        document.newPage();
        ItextpdfUtil.getWaterPic(writer);


        //创建定位符
        Anchor anchor1 = ItextpdfUtil.setAnchor("This is an anchor", "Back to Top");
        Paragraph paragraph1 = ItextpdfUtil.createParagraph("这是一段段落", sectionFont);
        paragraph1.add(anchor1);
        section1.add(paragraph1);


        Section section2 = ItextpdfUtil.createSection(chapter, "节1.2", sectionFont, 0);
        Phrase text2 = ItextpdfUtil.createPhrase("列表：", textFont);
        text2.setLeading(20); //上边距
        section2.add(text2);

        document.add(section2);

        //创建列表
        List list = ItextpdfUtil.createList(true, false, 20);
        String tmp = "文档是 PDF 文档的所有元素的容器";
        ListItem listItem1 = ItextpdfUtil.createListItem(tmp, textFont);
        ListItem listItem2 = ItextpdfUtil.createListItem("列表2", textFont);
        list.add(listItem1);
        list.add(listItem2);

        document.add(list);

        document.newPage();
        ItextpdfUtil.getWaterText(writer);
        //创建图像
        Image i = ItextpdfUtil.createImage("D:\\1.jpg");
        i.setScaleToFitHeight(true);
        i.setWidthPercentage(0.5F);
        list.add(i);
        section2.add(list);

        Section section3 = ItextpdfUtil.createSection(chapter, "节1.3", sectionFont, 0);
        Phrase text3 = ItextpdfUtil.createPhrase("表格：", textFont);
        section3.add(text3);

        //创建表格
        PdfPTable table = ItextpdfUtil.createPdfPTable(3);

        PdfPCell titleCell = ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("this is title")));
        titleCell.setColspan(3);
        table.addCell(titleCell);

        for (int j = 0; j < 5; j++) {
            table.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(i)));
            table.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("two" + j))));
            table.addCell(ItextpdfUtil.SetpdfPCellStyle(new PdfPCell(new Phrase("three" + j))));
        }

        section3.add(table);
        document.add(section3);

        document.newPage();
        ItextpdfUtil.getWaterText(writer);

        Section section4 = ItextpdfUtil.createSection(chapter, "节1.4", sectionFont, 0);
        Phrase text4 = ItextpdfUtil.createPhrase("定位符：", textFont);
        section4.add(text4);
        //引用定位符
        Anchor anchor2 = ItextpdfUtil.getAnchor("Back to Top", "Back to Top");
        section4.add(anchor2);
        document.add(section4);

        //关闭文档
        ItextpdfUtil.closeDocument(document);
        return "vvvvvv";
    }


    public static PdfPTable createTable(PdfWriter writer) {
        PdfPTable table = new PdfPTable(2);//生成一个两列的表格
        PdfPCell cell;
        int size = 20;
        cell = new PdfPCell(new Phrase("one"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("two"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("three"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("four"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("five"));
        cell.setColspan(1);//设置所占列数
        cell.setRowspan(2);//合并行
        cell.setFixedHeight(size * 2);//设置高度
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);//设置水平居中
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);//设置垂直居中
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("six"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        cell = new PdfPCell(new Phrase("seven"));
        cell.setFixedHeight(size);
        table.addCell(cell);
        return table;
    }

    public void createPDF(String filename) {
        Document document = new Document(PageSize.A4);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename));
            document.addTitle("example of PDF");
            document.open();
            PdfPTable table = createTable(writer);
            document.add(table);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }


}

